package com.qms.qep.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qms.common.core.controller.BaseController;
import com.qms.common.core.domain.AjaxResult;
import com.qms.common.core.page.TableDataInfo;
import com.qms.qep.domain.DefectRecord;
import com.qms.qep.service.IDefectRecordService;

/**
 * 缺陷记录Controller
 */
@RestController
@RequestMapping("/qep/defect-record")
public class DefectRecordController extends BaseController {
    @Autowired
    private IDefectRecordService defectRecordService;

    /**
     * 查询缺陷记录列表
     */
    @PreAuthorize("@ss.hasPermi('qep:defect-record:list')")
    @GetMapping("/list")
    public TableDataInfo list(DefectRecord defectRecord) {
        startPage();
        List<DefectRecord> list = defectRecordService.selectDefectRecordList(defectRecord);
        return getDataTable(list);
    }

    /**
     * 获取缺陷记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('qep:defect-record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(defectRecordService.selectDefectRecordById(id));
    }
    
    /**
     * 根据检验任务ID查询缺陷记录
     */
    @PreAuthorize("@ss.hasPermi('qep:defect-record:query')")
    @GetMapping(value = "/task/{inspectionTaskId}")
    public AjaxResult getListByTaskId(@PathVariable("inspectionTaskId") Integer inspectionTaskId) {
        return success(defectRecordService.selectDefectRecordByTaskId(inspectionTaskId));
    }

    /**
     * 新增缺陷记录
     */
    @PreAuthorize("@ss.hasPermi('qep:defect-record:add')")
    @PostMapping
    public AjaxResult add(@RequestBody DefectRecord defectRecord) {
        return toAjax(defectRecordService.insertDefectRecord(defectRecord));
    }

    /**
     * 修改缺陷记录
     */
    @PreAuthorize("@ss.hasPermi('qep:defect-record:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody DefectRecord defectRecord) {
        return toAjax(defectRecordService.updateDefectRecord(defectRecord));
    }

    /**
     * 删除缺陷记录
     */
    @PreAuthorize("@ss.hasPermi('qep:defect-record:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(defectRecordService.deleteDefectRecordByIds(ids));
    }
    
    /**
     * 统计缺陷记录数量
     */
    @PreAuthorize("@ss.hasPermi('qep:defect-record:query')")
    @GetMapping("/count")
    public AjaxResult count(DefectRecord defectRecord) {
        int count = defectRecordService.countDefectRecord(defectRecord);
        return success(count);
    }
} 